home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / ORBMECHD / ORBMECH1.C < prev    next >
C/C++ Source or Header  |  1991-07-30  |  11KB  |  539 lines

  1. /*******************************************
  2.  
  3.     Orbital Mechanics Application
  4.     Copyright Karl Dishaw 1990
  5.     
  6.     Version 0.0.1d
  7.     5 Jan 90
  8.     
  9.     Version 0.1.0d
  10.     17 Jul 90
  11.     
  12.     Version 0.5d
  13.     22 Oct 90
  14.     
  15.     Version 0.9a
  16.     18 Feb 91
  17.     
  18.     Version 0.95a
  19.     12 Jun 91
  20.     
  21.     Version 0.96a
  22.     27 Jun 91
  23.     
  24.     Written in Symantec Think C
  25.     
  26. ********************************************/
  27.  
  28. #include     "orbmech.h"
  29. #include    "SANE.h"
  30. #include    "math.h"
  31.  
  32. /* globals */
  33.  
  34. EventRecord        gEvent;
  35. Boolean            gifWNE, gDone = 1, gWriting = 0;
  36. DialogPtr        LambertDia, HohmannDia, HillsDia, KeplerDia, whichDialog, gHelpDia;
  37. DialogPtr        gLamHelpDia, gHillsHelpDia, gKepHelpDia, gHohHelpDia;
  38. Rect            gDragRect;
  39. Cursor            gEditCursor, gWaitCursor;
  40.                     
  41. WindowRecord    wRecord;
  42. WindowPtr        myWindow;    
  43. TEHandle        TEH;
  44. int                dirty = 0;                    
  45. ControlHandle     vScroll;
  46. MenuHandle        myMenus[6];
  47. Boolean            dialogEvent;
  48. decform            gdecform;
  49.  
  50. extern    Str255            gFileName;
  51.  
  52. struct    ParamRec
  53.     {
  54.     extended    value;
  55.     Str255        name;
  56.     }  gParameter;
  57.  
  58.  
  59.  
  60. /********************/
  61.  
  62. main()
  63. {
  64.     ToolBoxInit();
  65.     AppInits();
  66.     
  67.     MaxApplZone();
  68.     MoreMasters();
  69.     MoreMasters();
  70.     MoreMasters();
  71.     MoreMasters();
  72.     
  73.     gifWNE = ( NGetTrapAddress( WNE_TRAP_NUM, ToolTrap )
  74.             != NGetTrapAddress( UNIMPL_TRAP_NUM, ToolTrap ) );
  75.     
  76.     while ( gDone != FALSE )
  77.     {
  78.         EventLoop();
  79.     }
  80. }
  81.  
  82.  
  83. /************ Tool Box Init ********/
  84. ToolBoxInit()
  85. {
  86.     InitGraf( &thePort );
  87.     InitFonts();
  88.     FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
  89.     InitWindows();
  90.     InitMenus();
  91.     TEInit();
  92.     InitDialogs( NIL_POINTER );
  93.     InitCursor();
  94.     
  95.     gdecform.style = FLOATDECIMAL;
  96.     gdecform.digits = 8;
  97.  
  98. }    
  99.  
  100. /**************************/
  101.  
  102. AppInits()
  103. {
  104.     MenuBarInit();
  105.     WindowInit();
  106.     SetUpCursors();
  107.     SetUpDragRect();
  108.     SetUpFiles();
  109.     
  110.     DlgInits();
  111.  
  112.     gParameter.value = EARTH_CONSTANT;
  113.     pStrCopy("\pEarth", gParameter.name);
  114.     CheckItem( myMenus[PARAMETER_MENU], EARTH, TRUE);
  115.     
  116.     gdecform.style = FLOATDECIMAL;
  117.     gdecform.digits = 8;
  118. }
  119.  
  120. /**************************/    
  121.  
  122. SetUpCursors()
  123. {
  124.     CursHandle    hCurs;
  125.     
  126.     hCurs = GetCursor(3);
  127.     gEditCursor = **hCurs;
  128.     hCurs = GetCursor(watchCursor);
  129.     gWaitCursor = **hCurs;
  130. }    
  131.  
  132. /***************************/
  133.  
  134. SetUpDragRect()
  135. {
  136.     gDragRect = screenBits.bounds;
  137.     gDragRect.left += DRAG_THRESHOLD;
  138.     gDragRect.right -= DRAG_THRESHOLD;
  139.     gDragRect.bottom -= DRAG_THRESHOLD;
  140. }
  141.  
  142.     
  143. /*****************************/
  144.  
  145. MenuBarInit()
  146. {
  147.     Handle        myMenuBar;
  148.     
  149.     myMenuBar = GetNewMBar( 1000 );
  150.     SetMenuBar( myMenuBar );
  151.     myMenus[0] = GetMHandle( APPLE_MENU_ID );
  152.     AddResMenu( myMenus[0], 'DRVR' );
  153.     myMenus[1] = GetMHandle( FILE_MENU_ID );
  154.     myMenus[2] = GetMHandle( EDIT_MENU_ID );
  155.     myMenus[3] = GetMHandle( CALCULATIONS_MENU_ID );
  156.     myMenus[4] = GetMHandle( TUTORIALS_MENU_ID );
  157.     myMenus[5] = GetMHandle( PARAMETER_MENU_ID );
  158.     DrawMenuBar();
  159. }
  160.  
  161.  
  162. /********************/
  163.  
  164. EventLoop()
  165. {
  166.     char        theChar;
  167.     int            itemHit;
  168.     Rect        r;
  169.     WindowPtr    whichWindow;
  170.  
  171.     
  172.     if ( gifWNE )
  173.         WaitNextEvent( everyEvent, &gEvent, MIN_SLEEP, NIL_MOUSE_REGION );
  174.     else
  175.     {
  176.         SystemTask();
  177.         GetNextEvent( everyEvent, &gEvent );
  178.     }
  179.     
  180.     dialogEvent = IsDialogEvent( &gEvent );
  181.     
  182.     if ( dialogEvent && ( (gEvent.modifiers & cmdKey) == 0 ))
  183.     {
  184.         DialogSelect( &gEvent, &whichDialog, &itemHit );
  185.         
  186.         if ( ( whichDialog ) == LambertDia )
  187.                 HandleLambert( itemHit );
  188.         if ( ( whichDialog ) == HohmannDia )
  189.                 HandleHohmann( itemHit );
  190.         if ( ( whichDialog ) == HillsDia )
  191.                 HandleHills( itemHit );
  192.         if ( ( whichDialog ) == KeplerDia )
  193.                 HandleKepler( itemHit );
  194.         if ( ( whichDialog ) == gHelpDia )
  195.                 HandleHelp( itemHit );
  196.         if ( ( whichDialog ) == gLamHelpDia )
  197.                 HandleLamHelp( itemHit );
  198.         if ( ( whichDialog ) == gHillsHelpDia )
  199.                 HandleHillsHelp( itemHit );
  200.         if ( ( whichDialog ) == gKepHelpDia )
  201.                 HandleKepHelp( itemHit );
  202.         if ( ( whichDialog ) == gHohHelpDia )
  203.                 HandleHohHelp( itemHit );
  204.                 
  205.         if ( FindWindow( gEvent.where, &whichWindow ) == inMenuBar )
  206.             return( DoCommand( MenuSelect(gEvent.where) ) );
  207.     }
  208.     
  209.     if ((gEvent.modifiers & cmdKey) != 0  && ( gEvent.what == keyDown ) ) 
  210.         return( DoCommand( MenuKey( gEvent.message & charCodeMask ) ));
  211.     
  212.     if ( !dialogEvent )
  213.     {
  214.     switch ( gEvent.what )
  215.     {
  216.         case mouseDown:
  217.             HandleMouseDown();
  218.             break;    
  219.         case keyDown:
  220.         case autoKey: 
  221.             {
  222.                 register char    theChar;
  223.                 
  224.                 theChar = gEvent.message & charCodeMask;
  225.                 if ((gEvent.modifiers & cmdKey) != 0) 
  226.                     return( DoCommand( MenuKey( theChar ) ));
  227.                 else 
  228.                 {
  229.                     if ( gWriting && !dialogEvent )
  230.                     {
  231.                         TEKey( theChar, TEH );
  232.                         ShowSelect();
  233.                         dirty = 1;
  234.                     }
  235.                 }
  236.             }
  237.             break;
  238.         case activateEvt:
  239.             /* if (ours((WindowPtr)gEvent.message)) */
  240.             {    
  241.                 r=(*myWindow).portRect;
  242.                 r.top = r.bottom - (S_BAR_WIDTH+1);
  243.                 r.left = r.left - (S_BAR_WIDTH+1);
  244.                 InvalRect(&r);
  245.                 
  246.                 if ( gEvent.modifiers & activeFlag ) {
  247.                     TEActivate( TEH );
  248.                     gWriting = 1;
  249.                     ShowControl( vScroll );
  250.                 }
  251.                 else {
  252.                     TEDeactivate(TEH);
  253.                     gWriting = 0;
  254.                     HideControl( vScroll );
  255.                 }
  256.             }
  257.             break;
  258.         case updateEvt: 
  259.             /* if (ours((WindowPtr)gEvent.message)) */
  260.             UpdateWindow(myWindow);
  261.             break;
  262.         case nullEvent:
  263.             break;
  264.     }
  265.     }
  266.     
  267.     MaintainMenus();
  268.  
  269. }
  270.  
  271. /***************************/
  272.  
  273. HandleMouseDown()
  274. {
  275.     WindowPtr    whichWindow;
  276.     
  277.     switch (FindWindow( gEvent.where, &whichWindow )) 
  278.     {
  279.         case inSysWindow:
  280.             SystemClick( &gEvent, whichWindow );
  281.             break;
  282.         case inDrag:
  283.             DragWindow( whichWindow, gEvent.where, &gDragRect);
  284.             break;
  285.         case inGoAway:
  286.                          if (TrackGoAway( myWindow, gEvent.where) )
  287.                     DoFile( CLOSE_FILE );
  288.             break;
  289.         case inMenuBar:
  290.             return( DoCommand( MenuSelect(gEvent.where) ) );
  291.         case inGrow:
  292.             MyGrowWindow( whichWindow, gEvent.where );
  293.             break;
  294.         case inContent:
  295.             if (whichWindow != FrontWindow())
  296.                 SelectWindow(whichWindow);
  297.             else 
  298.                 DoContent(whichWindow, &gEvent);
  299.             break;
  300.     }
  301. }
  302.  
  303. ours(w)
  304. WindowPtr w;
  305. {
  306.     return( (myWindow!=NULL) && (w==myWindow) );
  307. }
  308.  
  309. /**********************************/
  310.  
  311. int DoCommand( mResult )
  312. long mResult;
  313. {
  314.     int            theItem, temp, accNumber;
  315.     Str255        accName;
  316.     WindowPeek     wPtr;
  317.     
  318.     theItem = LoWord( mResult );
  319.     switch (HiWord(mResult)) {
  320.     case APPLE_MENU_ID:
  321.         switch(theItem)
  322.         {
  323.             case ABOUT_ITEM:
  324.                 Alert( ABOUT_ALERT, NIL_POINTER);
  325.                 break;
  326.             default:
  327.                 GetItem( myMenus[0], theItem, accName );
  328.                 accNumber = OpenDeskAcc( accName );
  329.                 break;
  330.         }
  331.         break;
  332.     case FILE_MENU_ID: 
  333.         DoFile(theItem);
  334.         break;
  335.     case EDIT_MENU_ID: 
  336.         DoEdit(theItem);
  337.         break;
  338.     case CALCULATIONS_MENU_ID:
  339.         DoCalcMenu( theItem );
  340.         break;
  341.     case TUTORIALS_MENU_ID:
  342.         DoTutMenu( theItem );
  343.         break;
  344.     case PARAMETER_MENU_ID:
  345.         DoParameter( theItem );
  346.         break;
  347.     }
  348.     HiliteMenu(0);
  349.     return(1);
  350. }
  351.  
  352. /********************************/
  353.  
  354. DoEdit(theItem)
  355. int        theItem;
  356. {
  357.     WindowPeek wPtr;
  358.  
  359.     if (SystemEdit(theItem-1)==0) {
  360.         wPtr = (WindowPeek) FrontWindow();
  361.         switch (theItem) 
  362.         {
  363.         case CUT_COMMAND:
  364.             if( !dialogEvent ) {
  365.                 TECut( TEH );
  366.                 ZeroScrap();
  367.                 TEToScrap();
  368.                 dirty = 1;
  369.                 }
  370.             else {
  371.                 DlgCut( whichDialog );
  372.                 ZeroScrap();
  373.                 TEToScrap();
  374.                 }
  375.             break;
  376.         case COPY_COMMAND:
  377.             if( !dialogEvent ) {
  378.                 TECopy( TEH );
  379.                 ZeroScrap();
  380.                 TEToScrap();
  381.                 }
  382.             else  {
  383.                 DlgCopy( whichDialog );
  384.                 ZeroScrap();
  385.                 TEToScrap();
  386.                 }
  387.             break;
  388.         case PASTE_COMMAND:
  389.             if( !dialogEvent ) {
  390.                 TEFromScrap();
  391.                 TEPaste( TEH );
  392.                 dirty = 1;
  393.                 }
  394.             else  {
  395.                 TEFromScrap();
  396.                 DlgPaste( whichDialog );
  397.                 }
  398.             break;
  399. /**        case CLEAR_COMMAND:
  400.             if( !dialogEvent ) {
  401.                 TEDelete( TEH );
  402.                 }
  403.             else DlgDelete( whichDialog );
  404.             dirty = 1;
  405.             break;  **/
  406.         default: ;
  407.         }
  408.         
  409.         ShowSelect();
  410.     }
  411. }
  412.  
  413. /******************************/
  414.  
  415. DoParameter( theItem )
  416. int        theItem;
  417. {
  418.     SetItemMark( myMenus[PARAMETER_MENU], EARTH, 0 );
  419.     SetItemMark( myMenus[PARAMETER_MENU], SUN, 0 );
  420.     
  421.     switch ( theItem )
  422.     {
  423.         case 1:
  424.             gParameter.value = EARTH_CONSTANT;
  425.             pStrCopy("\pEarth", gParameter.name);
  426.             CheckItem( myMenus[PARAMETER_MENU], EARTH, TRUE);
  427.             break;
  428.         case 2:
  429.             gParameter.value = SUN_CONSTANT;
  430.             pStrCopy("\pSun", gParameter.name);
  431.             CheckItem( myMenus[PARAMETER_MENU], SUN, TRUE);
  432.             break;
  433.     }
  434. }
  435.  
  436.  
  437. pascal void ScrollProc(theControl, theCode)
  438. ControlHandle    theControl;
  439. int                theCode;
  440. {
  441.     int    pageSize;
  442.     int    scrollAmt;
  443.     
  444.     if (theCode == 0)
  445.         return ;
  446.     
  447.     pageSize = ((**TEH).viewRect.bottom-(**TEH).viewRect.top) / 
  448.             (**TEH).lineHeight - 1;
  449.             
  450.     switch (theCode) {
  451.         case inUpButton: 
  452.             scrollAmt = -1;
  453.             break;
  454.         case inDownButton: 
  455.             scrollAmt = 1;
  456.             break;
  457.         case inPageUp: 
  458.             scrollAmt = -pageSize;
  459.             break;
  460.         case inPageDown: 
  461.             scrollAmt = pageSize;
  462.             break;
  463.         }
  464.     SetCtlValue( theControl, GetCtlValue(theControl)+scrollAmt );
  465.     AdjustText();
  466.  
  467. }
  468.  
  469. DoContent(theWindow, theEvent)
  470. WindowPtr    theWindow;
  471. EventRecord    *theEvent;
  472. {
  473.     int                cntlCode;
  474.     ControlHandle     theControl;
  475.     int                pageSize;
  476.     GrafPtr            savePort;
  477.     
  478.     GetPort(&savePort);
  479.     SetPort(theWindow);
  480.     GlobalToLocal( &theEvent->where );
  481.     if ((cntlCode = FindControl(theEvent->where, theWindow, &theControl)) == 0) {
  482.         if (PtInRect( theEvent->where, &(**TEH).viewRect ))
  483.             TEClick( theEvent->where, (theEvent->modifiers & shiftKey )!=0, TEH);
  484.     }
  485.     else if (cntlCode == inThumb) {
  486.         TrackControl(theControl, theEvent->where, 0L);
  487.         AdjustText();
  488.     }
  489.     else
  490.         TrackControl(theControl, theEvent->where, &ScrollProc);
  491.  
  492.     SetPort(savePort);
  493. }
  494.  
  495. /***************************/
  496.  
  497. MaintainMenus()
  498. {
  499.     MenuHandle        temp;
  500.  
  501.     if ( !(*(WindowPeek)myWindow).visible || 
  502.             !ours(FrontWindow()) ) {
  503.         EnableItem( myMenus[FILE_MENU], NEW_FILE );
  504.         EnableItem( myMenus[FILE_MENU], OPEN_FILE );
  505.         DisableItem( myMenus[FILE_MENU], CLOSE_FILE );
  506.         DisableItem( myMenus[FILE_MENU], SAVE_FILE );
  507.         DisableItem( myMenus[FILE_MENU], SAVE_AS );
  508.         DisableItem( myMenus[FILE_MENU], REVERT_FILE );
  509.         EnableItem( myMenus[EDIT_MENU], CUT_COMMAND );
  510.         EnableItem( myMenus[EDIT_MENU], COPY_COMMAND );
  511.         /* EnableItem( myMenus[EDIT_MENU], clearCommand ); */
  512.     }
  513.     else {
  514.         DisableItem( myMenus[FILE_MENU], NEW_FILE );
  515.         DisableItem( myMenus[FILE_MENU], OPEN_FILE );
  516.         EnableItem( myMenus[FILE_MENU], CLOSE_FILE );
  517.         EnableItem( myMenus[FILE_MENU], SAVE_AS );
  518.         if (dirty && gFileName[0] != 0) {
  519.             EnableItem( myMenus[FILE_MENU], REVERT_FILE );
  520.             EnableItem( myMenus[FILE_MENU], SAVE_FILE );
  521.         }
  522.         else {
  523.             DisableItem( myMenus[FILE_MENU], REVERT_FILE );
  524.             DisableItem( myMenus[FILE_MENU], SAVE_FILE );
  525.         }
  526.  
  527.         if ((**TEH).selStart==(**TEH).selEnd) {
  528.             DisableItem( myMenus[EDIT_MENU], CUT_COMMAND );
  529.             DisableItem( myMenus[EDIT_MENU], COPY_COMMAND );
  530.             /* DisableItem( myMenus[EDIT_MENU], clearCommand ); */
  531.         }
  532.         else {
  533.             EnableItem( myMenus[EDIT_MENU], CUT_COMMAND );
  534.             EnableItem( myMenus[EDIT_MENU], COPY_COMMAND );
  535.             /* EnableItem( myMenus[EDIT_MENU], clearCommand ); */
  536.         }
  537.     }
  538. }
  539.